package com.example.administrator.rxjavarxandroid_retrofit.http.factory;

import android.net.ParseException;

import com.example.administrator.rxjavarxandroid_retrofit.http.BaseErrorBean;
import com.example.administrator.rxjavarxandroid_retrofit.http.exception.ApiError;
import com.example.administrator.rxjavarxandroid_retrofit.http.exception.CodeException;
import com.example.administrator.rxjavarxandroid_retrofit.http.exception.HttpTimeException;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

import org.json.JSONException;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import retrofit2.adapter.rxjava.HttpException;

/**
 * Created by Administrator on 2016/12/14.
 */
public class FactoryException {

    private static final String HttpException_MSG = "网络错误";
    private static final String ConnectException_MSG = "连接失败";
    private static final String JSONException_MSG = "fastjeson解析失败";
    private static final String UnknownHostException_MSG = "无法解析该域名";

    /**
     * 解析异常
     *
     * @param e
     * @return
     */
    public static ApiError analysisExcetpion(Throwable e) {
        ApiError apiException = new ApiError(e);
        if (e instanceof HttpException) {
             /*网络异常*/
            HttpException httpException = (HttpException) e;
            if (httpException.response() != null && httpException.response().errorBody() != null) {
                try {
                    String bodyStr = httpException.response().errorBody().string();
                    Gson gson = new Gson();
                    BaseErrorBean bean = gson.fromJson(bodyStr, BaseErrorBean.class);
                    if (bean != null) {
                        apiException.setCode(bean.getErrorCode());
                        apiException.setDisplayMessage(bean.getErrorDesc());
                    } else {
                        apiException.setCode(CodeException.HTTP_ERROR);
                        apiException.setDisplayMessage(HttpException_MSG);
                    }
                } catch (IOException e1) {
                    apiException.setCode(CodeException.HTTP_ERROR);
                    apiException.setDisplayMessage(e1.getMessage());
                }
            }
            } else if (e instanceof HttpTimeException) {
             /*自定义运行时异常*/
                HttpTimeException exception = (HttpTimeException) e;
                apiException.setCode(CodeException.RUNTIME_ERROR);
                apiException.setDisplayMessage(exception.getMessage());
            } else if (e instanceof ConnectException || e instanceof SocketTimeoutException) {
             /*链接异常*/
                apiException.setCode(CodeException.HTTP_ERROR);
                apiException.setDisplayMessage(ConnectException_MSG);
            } else if (e instanceof JSONException || e instanceof ParseException||e instanceof JsonSyntaxException) {
             /*fastjson解析异常e instanceof JSONPathException || */
                apiException.setCode(CodeException.JSON_ERROR);
                apiException.setDisplayMessage(JSONException_MSG);
            } else if (e instanceof UnknownHostException) {
            /*无法解析该域名异常*/
                apiException.setCode(CodeException.UNKOWNHOST_ERROR);
                apiException.setDisplayMessage(UnknownHostException_MSG);
            } else {
            /*未知异常*/
                apiException.setCode(CodeException.UNKNOWN_ERROR);
                apiException.setDisplayMessage(e.getMessage());
            }
            return apiException;
        }

    }


